/**
 * 
 */
package com.svms.sparedetails;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.svms.movement.MovementListModelItemRender;
import com.svms.service.vo.MovementVO;
import com.svms.service.vo.SpareCodesVO;
import com.svms.utils.SvmsMessageUtils;
import com.svms.web.constants.SvmsResources;
import com.svms.web.factory.SvmsWebFactory;

/**
 * @author om39a
 * 
 */
public class ManageSpareDetailCtrl extends GenericForwardComposer<Window> {

	private Window window_manage_spare;

	private Listbox lstbx_sparecodes;

	private Textbox txb_page_size;
	private Button btn_spare_help;
	private Button btn_spare_refresh;
	private Button btn_search_spare_list;
	private Button btn_add_spare;

	private ListModelList<SpareCodesVO> lst_model_sparecode;

	public Listbox getLstbx_Sparecodes() {
		return lstbx_sparecodes;
	}

	public void setLstbx_Sparecodes(Listbox lstbx_spares) {
		this.lstbx_sparecodes = lstbx_spares;
	}

	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent,
			ComponentInfo compInfo) {

		System.out.println("Spare doBeforeCompose ");
		loadSpareCodeToListModal();

		return super.doBeforeCompose(page, parent, compInfo);
	}

	@Override
	public void doAfterCompose(Window comp) throws Exception {
		System.out.println("Spare doAfterCompose ");
		super.doAfterCompose(comp);
	}

	private void loadSpareCodeToListModal() {
		List<SpareCodesVO> spareCodes = SvmsWebFactory.spareDetailsService()
				.getAllSparesDetailsList();
		System.out.println("%%%%");
		System.out.println(spareCodes);
		System.out.println("%%%");
		lst_model_sparecode = new ListModelList<SpareCodesVO>(spareCodes);

	}

	/**
	 * Populates the SpareCode details list
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onCreate$window_manage_spare(Event event) throws Exception {
		System.out.println("Spare onCreate$onCreate$window_manage_spare ");
		setSpareCodeListModalToListBox();
		setPageSize();
	}

	/**
	 * To set the paging size for the list
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onBlur$txb_page_size(Event event) throws Exception {
		setPageSize();
	}

	private void setSpareCodeListModalToListBox() {
		lstbx_sparecodes.setModel(lst_model_sparecode);
		lstbx_sparecodes.setMold("paging");
		lstbx_sparecodes.setItemRenderer(new ListitemRenderer<SpareCodesVO>() {

			@Override
			public void render(Listitem item, SpareCodesVO data, int index)
					throws Exception {

				final SpareCodesVO spareCode = (SpareCodesVO) data;

				Listcell lc = new Listcell(spareCode.getSpareName());
				lc.setParent(item);
				lc = new Listcell(spareCode.getValidationFormat());
				lc.setParent(item);
				lc = new Listcell(spareCode.getMaxDays());
				lc.setParent(item);
				lc = new Listcell(spareCode.getCutOffDays());
				lc.setParent(item);
				lc = new Listcell(spareCode.getMaxDistance());
				lc.setParent(item);
				lc = new Listcell(spareCode.getCutOffDistance());
				lc.setParent(item);

				item.setAttribute("selectedSpareDetail", data);

				ComponentsCtrl.applyForward(item,
						"onDoubleClick=onSpareDetailItemDoubleClicked");

			}
		});

		btn_add_spare.setDisabled(false);
		btn_spare_help.setDisabled(false);
		btn_spare_refresh.setDisabled(false);
		btn_search_spare_list.setDisabled(false);

		txb_page_size.setDisabled(false);
		txb_page_size.setValue(null);
		txb_page_size.setMaxlength(2);
	}

	private void setPageSize() {
		if (StringUtils.isNotBlank(txb_page_size.getValue())) {
			try {
				lstbx_sparecodes.setPageSize(Integer.valueOf(txb_page_size
						.getValue()));
			} catch (Exception e) {
				SvmsMessageUtils
						.showErrorMessage("Page size can only be a number");
			}
		} else {
			lstbx_sparecodes.setPageSize(15);
		}
	}

	/**
	 * To reset the vehicle list to current values in database
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_spare_refresh(Event event) throws Exception {
		loadSpareCodeToListModal();
		setSpareCodeListModalToListBox();
	}
	
	/**
	 * To make a add vehicle call
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_add_spare(Event event) throws Exception {
		showDetailView(null);
	}

	/**
	 * To display help menu
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_spare_help(Event event) throws Exception {
		StringBuffer helpMessage = new StringBuffer();
		helpMessage
				.append("---------------------------------------------------------");
		helpMessage.append("\n");
		helpMessage.append("1. Add Vehicle button to add a new vehicle");
		helpMessage.append("\n");
		helpMessage.append("2. Search to find avehicle in system");
		helpMessage.append("\n");
		helpMessage.append("3. Refresh to get a latest update from system");
		helpMessage.append("\n");
		helpMessage.append("4. Double click on the vehicle in list to edit it");
		helpMessage.append("\n");
		helpMessage
				.append("---------------------------------------------------------");
		SvmsMessageUtils.showGenericMessage(helpMessage.toString());
	}

	/**
	 * To search for vehicles from the main vehicle window
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onClick$btn_search_spare_list(Event event) throws Exception {
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("spareDetailsCtrl", this);
		Window win = null;
		try {
			win = (Window) Executions.createComponents(
					SvmsResources.SPARE_SEARCH_DIALOGUE, null, args);
		} catch (final Exception e) {
			e.printStackTrace();
			SvmsMessageUtils.showErrorMessage(e.getMessage());
			if (win != null) {
				win.detach();
			}
		}
	}

	/**
	 * To perform the double click event on the list items
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onSpareDetailItemDoubleClicked(Event event) throws Exception {

		Listitem item = this.lstbx_sparecodes.getSelectedItem();

		if (item != null) {
			SpareCodesVO spareCode = (SpareCodesVO) item
					.getAttribute("selectedSpareDetail");
			System.out.println("...............");
			System.out.println(spareCode);
			System.out.println("...............");
			showDetailView(spareCode);
		}

	}

	/**
	 * Opens the detail view of the Vehicle Controller. <br>
	 * Overhanded some params in a map if needed. <br>
	 * 
	 * @param branche
	 * @throws Exception
	 */
	private void showDetailView(SpareCodesVO spareCode) throws Exception {

		final HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("selectedSpareCode", spareCode);
		map.put("spareDetailsCtrl", this);
		Window win = null;
		try {
			win = (Window) Executions.createComponents(
					SvmsResources.MANAGE_SPARE_DIALOGUE, null, map);
		} catch (final Exception e) {
			e.printStackTrace();
			SvmsMessageUtils.showErrorMessage(e.getMessage());
			if (win != null) {
				win.detach();
			}
		}
	}

}
